from odoo import models, fields, api


class SbPlanOverview(models.Model):
    _name = 'sb.plan.overview'
    _description = 'Plan Overview'

    name = fields.Char('Name', default="", help="Name", translate=True)
    code = fields.Char(string='Code', required=True, copy=False, ondelete='')
    color = fields.Integer('Color', default="1")
    count_waiting = fields.Integer('QTY', compute="_compute_count_waiting")

    @api.depends('code')
    def _compute_count_waiting(self):
        for rec in self:
            if rec.code == 'sale_plan':
                rec.count_waiting = self.env['sale.order.line'].search_count([
                    ('product_id.detailed_type', '=', 'product'),
                    ('state', '=', 'sale'),
                    ('s_remaining_planned_qty', '>', 0),
                    ('s_plan_close', '=', False)
                ])
            elif rec.code == 'rd_plan':
                rec.count_waiting = self.env['s.research.and.development.order.line'].search_count([
                    ('remaining_planned_qty', '>', 0),
                    ('state', 'in', ('confirm', 'part_feed'))
                ])
            else:
                rec.count_waiting = 0

    def get_plan_model_domain(self):
        self.ensure_one()
        domain = []
        if self.code == 'mrp':
            domain = [('state', 'not in', ('done', 'cancel'))]
        elif self.code == 'rd':
            domain = [('state', 'not in', ('done_feed', 'cancel'))]
        return domain

    def get_plan_waiting_ids(self):
        self.ensure_one()
        domain = self.get_plan_model_domain() or []
        return self.open_view()

    def get_plan_done_ids(self):
        self.ensure_one()
        domain = self.get_plan_model_domain() or []
        return self.open_view()

    def get_plan_overview_action(self):
        self.ensure_one()
        return self.open_view()

    def open_view(self):
        self.ensure_one()
        if self.code == 'mrp':
            action = self.env["ir.actions.actions"]._for_xml_id("mrp.mrp_production_action")
            action.update({'domain': []})
            return action
        elif self.code == 'rd':
            return self.env["ir.actions.actions"]._for_xml_id("sb_engineering.s_research_and_development_order_action")
        elif self.code == 'fs':
            return self.env["ir.actions.actions"]._for_xml_id("pcbmanufact.action_sb_mo_scrap_report")
        elif self.code == 'sale_plan':
            return self.env["ir.actions.actions"]._for_xml_id("pcbmanufact.action_sale_order_line_select_to_wizard")
        elif self.code == 'rd_plan':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.research_and_development_order_line_action")
        elif self.code == 'wip':
            return self.env["ir.actions.actions"]._for_xml_id("sb_mrp_workorder_report.action_mrp_workorder_report")
        elif self.code == 'production_material':
            return self.env["ir.actions.actions"]._for_xml_id("pcbmanufact.s_production_material_demand_action")
        elif self.code == 'material_requirements':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.s_material_requirements_analysis_action")
        elif self.code == 'wip2':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.s_production_wip_mass_two_action")
        elif self.code == 'production_daily':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.s_production_daily_report_two_action")
        elif self.code == 'fs_two':
            return self.env["ir.actions.actions"]._for_xml_id("pcbmanufact.action_s_mrp_base")
        elif self.code == 'sample_wip':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.s_production_sample_wip_two_action")
        elif self.code == 'sale_material_preparation':
            return self.env["ir.actions.actions"]._for_xml_id("sb_plan.action_s_material_preparation_sale_line_report")
        elif self.code == 'rework_and_maintenance':
            return self.env["ir.actions.actions"]._for_xml_id("sb_quality_check.s_rework_check_act_window")
        else:
            return False
